﻿#region Includes
using System;
using System.Text;
using System.Drawing;
using OpenTK;
using OpenTK.Graphics.OpenGL;
#endregion

namespace GingerGL.Graphics
{
    //------------------------------------------------------------//
    /// <summary>
    /// 2D Text Class
    /// </summary>
    public class Text2D : Text
    {
        //--------------------------------------------------------//
        #region Constructors
        /// <summary>
        /// Initialize new instance
        /// </summary>
        public Text2D(string path) : base(Image.FromFile(path)) { }
        /// <summary>
        /// Initialize new instance
        /// </summary>
        public Text2D(Image img) : base(img) { }
        #endregion
        //--------------------------------------------------------//
        #region Methods
        /// <summary>
        /// Gets Text2D to string
        /// </summary>
        public override string ToString()
        {
            return "Text2D";
        }
        /// <summary>
        /// Draws Text to Screen
        /// <para>Screen is projected to a 640x480 resolution</para>
        /// </summary>
        public void Draw(float x, float y, string message) 
        { 
            this.Draw(new Vector3(x, y, 0), message, 0); 
        }
        /// <summary>
        /// Base Class Override
        /// </summary>
        protected override void Draw(Vector3 position, string message, int charset)
        {
            if (charset > 1) { charset = 1; }
            GL.Enable(EnableCap.Blend);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.One);
            this.Font.Apply();                                                          // Select Our Font Texture
            GL.Disable(EnableCap.DepthTest);                                            // Disables Depth Testing
            GL.MatrixMode(MatrixMode.Projection);                                       // Select The Projection Matrix
            GL.PushMatrix();                                                            // Store The Projection Matrix
            GL.LoadIdentity();                                                          // Reset The Projection Matrix
            GL.Ortho(0, 640, 480, 0, -1, 1);                                            // Set Up An Ortho Screen
            GL.MatrixMode(MatrixMode.Modelview);                                        // Select The Modelview Matrix
            GL.PushMatrix();                                                            // Store The Modelview Matrix
            GL.LoadIdentity();                                                          // Reset The Modelview Matrix
            GL.Translate(position);                                                     // Position The Text (0,0 - Bottom Left)
            GL.ListBase(this.Base - 32 + (128 * charset));                              // Choose The Font Set (0 or 1)
            GL.CallLists(
                message.Length, 
                ListNameType.UnsignedByte, 
                Encoding.ASCII.GetBytes(message));                                      // Write The Text To The Screen
            GL.MatrixMode(MatrixMode.Projection);                                       // Select The Projection Matrix
            GL.PopMatrix();                                                             // Restore The Old Projection Matrix
            GL.MatrixMode(MatrixMode.Modelview);                                        // Select The Modelview Matrix
            GL.PopMatrix();                                                             // Restore The Old Projection Matrix
            GL.Enable(EnableCap.DepthTest);                                             // Enables Depth Testing
            GL.Disable(EnableCap.Blend);
        }
        #endregion
        //--------------------------------------------------------//
    }
    //------------------------------------------------------------//
}
